package com.whty.cms.filewps.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

public class DESEncrypt {

    private static final byte[] DESkey = "66666666".getBytes();// 设置密钥，略去
    private static final byte[] DESIV = "88888888".getBytes() ;// 设置向量，略去
    //加密算法的参数接口，IvParameterSpec是它的一个实现
    static AlgorithmParameterSpec iv = getIv();
    private static Key key = getKey();




    private static AlgorithmParameterSpec getIv() {
        AlgorithmParameterSpec iv = null;
        try {
            // 设置向量
            iv = new IvParameterSpec(DESIV);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return iv;
    }

    private static Key getKey(){
        Key key = null;
        // 设置密钥参数
        try {
        DESKeySpec keySpec = new DESKeySpec(DESkey);
        // 获得密钥工厂
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        key = keyFactory.generateSecret(keySpec);// 得到密钥对象
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return key;
    }
    /**
     * @param data 加密
     * @return
     * @throws
     */
    public static String encode(String data) throws Exception {
        // 得到加密对象Cipher
        StringBuilder sb = new StringBuilder();
        if(data.startsWith("https")){
            sb.append("ssl=1&n=1&furl=");
        }else{
            sb.append("n=1&furl=");
        }
        Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        // 设置工作模式为加密模式，给出密钥和向量
        enCipher.init(Cipher.ENCRYPT_MODE, key, iv);
        byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
        return sb.append(Base64.encodeBase64String(pasByte).replaceAll("\\+", "_").replaceAll("\\/", "@")).toString();
    }
    /**
     * @param data 解密
     * @return
     * @throws
     */
    public static String decode(String data) throws Exception {
        Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        deCipher.init(Cipher.DECRYPT_MODE, key, iv);
        byte[] pasByte = deCipher.doFinal(Base64.decodeBase64(data.replaceAll("_", "+").replaceAll("@", "/")));
        return new String(pasByte, "UTF-8");
    }

    public static void main(String[] args) throws Exception {
        //DESEncrypt tools = new DESEncrypt();
        System.out.println("加密:" + encode("http://whty-ypt-source-1256736654-cdn.huijiaoyun.com/888888/gfs_mnt/data1/2022/7/22/9/33/2db48af5257445edb304825a5572d34d/6cc3e67fc99543c6b863a197cff9bf82/Do%20you%20often%20tidy%20your%20bed%EF%BC%9F.ppt?sign=q-sign-algorithm%3Dsha1%26q-ak%3DAKIDVwpSshEZaL2YwDe5ernIahZLYqsotxYu%26q-sign-time%3D1658453647%3B7965653647%26q-key-time%3D1658453647%3B7965653647%26q-header-list%3Dhost%26q-url-param-list%3Dresponse-content-disposition%26q-signature%3Dea83d10985b55ac6db8207ab4845eb051490bff4&response-content-disposition=attachment"));
        //System.out.println("解密:" + decode(encode("http://filecenter.huijiaoyun.com/filecenter/upload/download?fid=1259185318663380992")));
    }
}
